<?php
namespace Wx\Controller;
use Think\Controller;
use Wx\Model\shareModel;


class WishGroupController extends CommonController {

    public function _initialize(){
        parent::_initialize();
        $order = M('wish_order');
        // 查询数据库中未付款的订单
        $list1 = $order->where('pay_status = 0')->select();
        if(!empty($list1)){
            foreach ($list1 as $k=>$v){
                $endtime = $v['addtime']+60*60*24-time();   // 用户看到的开团截止日期
                if($endtime<=0){//如果超过截止日期，删除订单
                    $order->delete($v['id']);
                }
            }
        }
        // 查询数据库中已付款-未成团的订单
        $list2 = $order->where('pay_status = 1 AND status = 0')->select();
        $member =  M('member');
        if(!empty($list2)){
            foreach ($list2 as $v){
                $endtime = $v['addtime']+60*60*24-time();
                if($endtime<=0){//如果超过截止日期，直接成团
                    $member->where(array('id'=>$v['u_id']))->setInc('credit1',18); //积分
                    $member->where(array('id'=>$v['u_id']))->setInc('credit2',18); //卡额
                    //增加积分变动记录
                    changeCredit($v['u_id'],'购买心愿卡','credit1','+',18);
                    //增加余额变动记录
                    changeCredit($v['u_id'],'购买心愿卡','credit2','+',18);
                    //修改心愿团状态
                    $order->where(array('id'=>$v['id']))->setField('status',1);
                }
            }
        }

    }//end fun


    // 心愿团首页
    public function index(){
        // 查询数据库中，用户有没有参团
        $order_info = getInfo('wish_order',array('u_id'=>session('u_id')));
        // 判断是否为分享链接进入，share_id 为分享人id
        $share_id = intval(I('share_id'));
        if($share_id){
            $user_name = M('member')->where('id='.$share_id)->getField('nickname'); //分享人昵称
            $share_order = getInfo('wish_order',array('u_id'=>$share_id));          //分享人的心愿团订单
            $share_wish = getInfo('wish_group',array('id'=>$share_order['ws_id']));
            if($share_order['status']==0){//如果分享人参与的心愿团未成团
                $order_list = M('wish_order')->where(array('ws_id'=>$share_order['ws_id'],'pay_status'=>1))->select();
                foreach ($order_list as $k=>$v){//查询与用户同一个团的成员信息
                    $share_group[$k] = M('member')->where('id='.$v['u_id'])->field('nickname,avatar,hometown')->find();
                }
                $this->assign('share_group',$share_group);
            }
            $this->assign('user_name',$user_name);
            $this->assign('share_wish',$share_wish);
        }
        // 判断是否为支付成功后进入
        $pay = intval(I('pay'));
        if($pay){
            $c_id = M('member')->where('id='.session('u_id'))->getField('c_id');
            if($c_id==0){
                $this->assign('pay',$pay);
            }
        }
        if(empty($order_info)){
            // 查询数据库中，有没有待成的心愿团
            $wish_info = getInfo('wish_group',array('status'=>0));
            if(!empty($wish_info)){
                $order_list = M('wish_order')->where(array('ws_id'=>$wish_info['id'],'pay_status'=>1))->select();
                foreach ($order_list as $k=>$v){//查询与用户同一个团的成员信息
                    $member_list[$k] = M('member')->where('id='.$v['u_id'])->field('nickname,avatar,hometown')->find();
                }
                $this->assign('member_list',$member_list);
            }
        }else{
            $order_info['endtime'] = $order_info['addtime']+60*60*24-time();   // 用户看到的开团截止日期
//            dump(date('Y-m-d h:i',$order_info['addtime']));
            // 用户参与的心愿团
            $wish_info = getInfo('wish_group',array('id'=>$order_info['ws_id']));
            // 与用户同一个团的订单
            $order_list = M('wish_order')->where(array('ws_id'=>$wish_info['id'],'pay_status'=>1))->select();
            foreach ($order_list as $k=>$v){//查询与用户同一个团的成员信息
                $member_list[$k] = M('member')->where('id='.$v['u_id'])->field('nickname,avatar,hometown')->find();
                $m_id[''] = $v['u_id'];
            }
            $m_num = count($member_list);
            //当过了二十四小时，用户参与的心愿团还未成团时，程序会直接让用户显示成团，这时参团人数是不满五人的，需要随机从数据库中查询几条用户信息来让成团人数显示五人
            if($order_info['status']==1&&$wish_info['status']!=1){
                $list = session('member_list');
                if(empty($list)){
                    $num = intval(5-$m_num);
                    $where['id'] = array('not in',$m_id);
                    $where['nickname'] = array('neq','');
                    $where['avatar'] = array('neq','');
                    $member = M('member')->where($where)->field('nickname,avatar,hometown')->order( "rand()" )->limit($num)->select();
                    $member_list = array_merge($member_list,$member);
                    session('member_list',$member_list);
                }else{
                    $member_list = $list;
                }
            }
            $this->assign('order_info',$order_info);
            $this->assign('member_list',$member_list);
        }// end if
        $number = intval(M('wish_group')->sum("person"));   //心愿团参与人数
        // 查询试吃试用商品
        $foretaste = M("foretaste_goods")->where('state = 1')->order('starttime ASC,addtime DESC')->limit(4)->select();
        //微信分享
        $wxshare=new shareModel(C('WX_API.appid'),C('WX_API.secret'));
        $signPackage=$wxshare->getSignPackage();
        $this->assign('signPackage',$signPackage);//微信数据

        //获取统计数据
        $count_result = $this->groupInfo(session('u_id'));

        $this->assign('share_id',session('u_id'));
        $this->assign('wish_info',$wish_info);
        $this->assign('number',$number);
        $this->assign("foretaste",$foretaste);
        $this->assign($count_result);
        $this->display('prodetail-xyt');
    }// end fun index()

    //心愿团确认订单-页面
    public function confirm(){
        // 直接从数据库-查询待成团的心愿团id
        $ws_id =   M('wish_group')->where(array('status'=>0))->getField('id');
        if($ws_id){// 如果有待成得心愿团，查询参团人的信息
            $allorder = M('wish_order')->where(array('ws_id'=>$ws_id,'pay_status'=>1))->field('u_id')->select();  //心愿团-订单信息
            // 订单信息中所有的用户信息
            foreach($allorder as $k => $v){
                $member[$k] = getMember($v['u_id'],'nickname,avatar,hometown');
            }// end foreach
            $wish_info = getInfo('wish_group',array('id'=>$ws_id));
            $this->assign('wish_info',$wish_info);
            $this->assign('member',$member);
        }
        $this->display('confirmOrder-xyt');

    }// end fun confirm()

    //心愿团确认订单-提交
    public function confirmOrder(){
//        $data['ws_id'] = intval(I('ws_id')); //心愿团表id，值可能为空
        // 直接从数据库-查询待成团的心愿团id
        $data['ws_id'] =   M('wish_group')->where(array('status'=>0))->getField('id');
        if(!$data['ws_id']){
            $data['ws_id'] = 0;
        }
        $data['u_id'] = session('u_id');    // 用户id
        $data['order_sn'] = 'Ws'.date('YmdHis') . str_pad(mt_rand(1, 99999), 5, '0', STR_PAD_LEFT); //订单编号
        $data['addtime'] = time();          // 下单时间
        $data['total_fee'] = 9;             //支付金额
        $result = M('wish_order')->add($data);     //新增数据id
        // 组合url
        $url = 'http://www.xiangtws.com/Api/Weixinpay/wishpay?order_id='.$result;
        // 前往支付
        header('location:'.$url);
    }// end fun confirmOrder()


    // 心愿团拼团信息
    public function groupInfo($u_id){
        // 查询全国哪些地区参与了心愿团
        $wish = M('wish_order');
        $group_list = $wish->field('u_id,id')->select();
        $user_c_id = M('member')->where('id='.$u_id)->getField('c_id');
        foreach ($group_list as $k=>$v){
            $c_id = M('member')->where('id='.$v['u_id'])->getField('c_id'); //用户小区id
            if($c_id!=0){
                $province_id = M('shop_community')->where('id='.$c_id)->getField('a_id_1');  //心愿团用户小区所在市id
                // 判断是否是直辖市
                if($province_id==1||$province_id==7269||$province_id==155306||$province_id==534059){
                    $city_id = $province_id;
                }else{
                    $city_id = M('shop_community')->where('id='.$c_id)->getField('a_id_2');  //心愿团用户小区所在市id
                }
                $info[$k]['city'] =  $city_id;  //心愿团用户小区所在市id
                $info[$k]['c_id'] =  $c_id;  //心愿团用户小区id
                if($user_c_id==$c_id){//统计与“我”同小区的用户的家乡
                    $hometown[]  = M('member')->where('id='.$v['u_id'])->getField('hometown');
                }
            }// end if
        }// end foreach

        //去除二位数组中重复的元素
        $info = unique_arr($info);
        foreach ($info as $v){//市名称
            $info1[] = M('area')->where('id='.$v['city'])->getField('name');
        }// end foreach
        //统计每个市有多少小区参与
        $result['comm_count'] = array_count_values($info1);
        //“我”的社区有多少人参与了心愿团
        $result['comm_number'] = count($hometown);
        // 统计与“我”同小区的用户的家乡出现的次数
        $result['hometown_count'] = array_count_values($hometown);
        // 我的小区名称
        $result['comm_title'] = M('shop_community')->where('id='.$user_c_id)->getField('title');
        return $result;

    }// end fun groupInfo()


}//end class